package com.scaffold.authority.action;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import wangjg.commons.page.Page;

import com.scaffold.BasicBizAction;
import com.scaffold.authority.entity.Role;
import com.scaffold.authority.service.RoleService;

@Controller
public class RoleJsonAction extends BasicBizAction{
	@Autowired
	private RoleService roleService;

	@RequestMapping("/role/list.json")
	public String list(Integer type, String name, String intro, String keyword){
		Page page = this.getPage(1);
		List<Role> list = roleService.pageQuery(page, type, name, intro, keyword);
		return this.getPageResult(page, list);
	}
	
	@RequestMapping("/role/save.json")
	public String save(Long id){
		if(id==null){
			Role role = new Role();
			this.bindParams(role);
			this.setOwner(role);
			this.roleService.save(role);
		}else{
			Role role = this.roleService.getRole(id);
			this.checkOwner(role);
			this.bindParams(role);
			this.roleService.update(role);
		}
		return this.getSuccessResult();
	}

	@RequestMapping("/role/get.json")
	public String get(Long id){
		Role role = roleService.getRole(id);
		this.checkOwner(role);
		return this.getSuccessResult(role);
	}

	@RequestMapping("/role/delete.json")
	public String delete(Long id){
		Role role = roleService.getRole(id);
		this.checkOwner(role);
		this.roleService.delete(role);
		return this.getSuccessResult();
	}


	@RequestMapping("/role/batchDelete.json")
	public String batchDelete(String idseq){
		this.roleService.batchDelete(idseq);
		return this.getSuccessResult();
	}

	@RequestMapping("/role/select.json")
	public String select(Integer type, String name, String intro, String keyword){
		List<Role> list = roleService.pageQuery(null, type, name, intro, keyword);
		return this.getSuccessResult(list);
	}


	@RequestMapping("/role/setting/saveAuthoritys.json")
	public String saveAuthoritys(Long roleId, String authorityId) {
		Role role = this.roleService.getRole(roleId);
		this.checkOwner(role);
		roleService.saveAuthoritys(roleId, authorityId);
		return this.getSuccessResult();
	}

	@RequestMapping("/role/setting/getAuthorityIdList.json")
	public String getAuthorityIdList(Long roleId) {
		Role role = this.roleService.getRole(roleId);
		this.checkOwner(role);
		List<Long> list = roleService.getAuthorityIdList(roleId);
		return this.getSuccessResult(list);
	}
}
